package com.lfy.config;

import com.lfy.modules.common.intercept.AuthHandlerInterceptor;
import com.lfy.modules.common.anno.AutoUser;
import com.lfy.modules.common.domain.entity.UserEntity;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 *  通过参数自动注入user
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/3 20:36
 */
@Component
public class ParamsConfig implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {

        return  methodParameter.getParameterType().equals(UserEntity.class) &&
                methodParameter.hasParameterAnnotation(AutoUser.class);
    }

    @Override
    public UserEntity resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return AuthHandlerInterceptor.authLocal.get();
    }
}
